package com.xszx.knowledge.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xszx.knowledge.dto.DocReq;
import com.xszx.knowledge.dto.PageReq;
import com.xszx.knowledge.entity.Content;
import com.xszx.knowledge.entity.Doc;
import com.xszx.knowledge.entity.Ebook;
import com.xszx.knowledge.exception.DevException;
import com.xszx.knowledge.exception.DevExceptionCode;
import com.xszx.knowledge.service.ContentService;
import com.xszx.knowledge.service.DocService;
import com.xszx.knowledge.util.RedisUtil;
import com.xszx.knowledge.util.RequestContext;
import com.xszx.knowledge.util.Result;
import io.swagger.annotations.Api;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 文档 前端控制器
 * </p>
 *
 * @author wencai
 * @since 2023/04/25 10:29
 */
@Api(tags = "文档管理")
@RestController
@RequestMapping("/doc")
public class DocController {
    @Resource
    private DocService docService;
    @Resource
    private ContentService contentService;


    /**
     * 查询全部的数据
     * @return
     */
    @GetMapping("/getAll")
    public Result getAllDoc(long ebookId){
        ebookId = (ebookId == 0)?null:ebookId;
        return Result.ok(docService.list(new QueryWrapper(new Doc(ebookId))));
    }

    /**
     * 进行数据分页的查询
     * @return
     */
    @GetMapping("/getPage")
    public Result getPageDoc(PageReq pageReq){
        //指定当前页和每页显示数据的条数
        Page page = new Page(pageReq.getCurrent(),pageReq.getSize());
        //进行数据的查询
        IPage<Doc> docIPage = docService.page(page);
        return Result.ok(docIPage);
    }

    /**
     * 定义添加或者更新的接口
     */
    @PostMapping("/save")
    public Result save(@RequestBody DocReq docReq){
        Result reuslt =Result.ifSuccess(docService.saveDocAndContent(docReq));
        return reuslt;
    }
    /**
     * 定义删除父子文档接口
     */
    @PostMapping("/delete/{ids}")
    public Result delete(@PathVariable String ids){
       return Result.ifSuccess(docService.removeBatchByIds( Arrays.asList(ids.split(","))));
    }

    /**
     * 定义删除父子文档接口
     */
    @GetMapping("/vote/{id}")
    public Result vote(@PathVariable long id){
        docService.vote(id);
        return Result.ok();
    }

}
